package com.base.kongjian.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import cn.mysteel.util.ObjectUtils;

import com.base.kongjian.dao.CommentDAO;
import com.base.kongjian.entity.Comment;
import com.base.kongjian.service.CommentService;
import com.mysteel.page.Page;

@Service("commentService")
public class CommentServiceImpl implements CommentService
{
	@Autowired
	private CommentDAO commentDao;
	
	@Override
	public void comment(Comment comment)
	{
		Assert.notNull (comment);

		comment.setCommentTime (System.currentTimeMillis ());
		
		commentDao.save (comment);
	}

	@Override
	public void deleteCommentById(long id)
	{
		Comment comment = commentDao.get (id);
		
		if(ObjectUtils.notNull (comment))
		{
			commentDao.delete (id);
		}
	}

	@Override
	public Comment getCommentById(long id)
	{
		
		return commentDao.get (id);
	}

	@Override
	public Page<Comment> queryComment(Map<String, Object> params,int currentPageNum, int maxPageRowCount)
	{
		Page<Comment> page = null;

		if (ObjectUtils.isNull(params))
		{
			params = new HashMap<String, Object>(2);
		}
		
		int count = commentDao.count(params);

		if (count > 0)
		{
			page = new Page<Comment>();
			page.setTotalRowCount(count);
			page.setCurrentPageNum(currentPageNum);
			page.setMaxPageRowCount(maxPageRowCount);
			page.setURL(params);

			params.put("startPageRowCount", page.getStartPageRowCount());
			params.put("maxPageRowCount", page.getMaxPageRowCount());

			List<Long> comIds = commentDao.query(params);

			if (ObjectUtils.notEmpty(comIds))
			{
				List<Comment> comms = new ArrayList<Comment>(comIds.size());

				for (long cId : comIds)
				{
					comms.add(getCommentById (cId));
				}

				page.setDatas(comms);
			}
		}

		return page;
		
	}
}
